package cn.com.shopec.erp.stock.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ISupplierInfoDao;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.IntegerUtil;
import cn.com.shopec.erp.product.dao.ICommodityDao;
import cn.com.shopec.erp.product.dao.IProductDao;
import cn.com.shopec.erp.stock.commo.ObjectUtil;
import cn.com.shopec.erp.stock.dao.IStockHistoryDao;
import cn.com.shopec.erp.stock.entity.StockHistory;
import cn.com.shopec.erp.stock.service.IStockHistoryService;
import cn.com.shopec.erp.stock.vo.StockHistoryVo;

/**
 * @ClassName: StockHistoryServiceImpl
 * @Description: 库存变更记录service接口实现类
 * @author: zengxuezhou
 * @date: 2016年11月28日 下午2:27:36
 */
@Service
public class StockHistoryServiceImpl implements IStockHistoryService {

	@Resource
	private IStockHistoryDao stockHistoryDao;

	@Resource
	private ICommodityDao commodityDao;

	@Resource
	private IProductDao productDao;

	@Resource
	private ISupplierInfoDao supplierInfoDao;

	@Override
	public PageFinder<StockHistoryVo> queryStockHistoryList(StockHistoryVo stockHistory, PageQuery pageQuery) {
		Map<String, Object> values = new HashMap<String, Object>();
		String criteriaSql = stockHistoryDao.getQueryCriteriaSql(stockHistory, values);
		int count = stockHistoryDao.getProductsCountByStockNo(criteriaSql, values);
		PageFinder<StockHistoryVo> result = new PageFinder<StockHistoryVo>(pageQuery.getPage(),pageQuery.getPageSize(), count);
		if (IntegerUtil.gtZero(count)) {
			List<StockHistoryVo> stockHistoryVos = stockHistoryDao.getStockHistorys(criteriaSql, values,result.getStartOfPage(),result.getPageSize());
			result.setData(stockHistoryVos);
		}
		return result;
	}
	
	@Override
	public List<StockHistoryVo> queryStockHistoryList(StockHistoryVo stockHistory) {
		Map<String, Object> values = new HashMap<String, Object>();
		String criteriaSql = stockHistoryDao.getQueryCriteriaSql(stockHistory, values);
		int count = stockHistoryDao.getProductsCountByStockNo(criteriaSql, values);
		if (IntegerUtil.gtZero(count)) {
			return stockHistoryDao.getStockHistorys(criteriaSql, values);
		}
		return null;
	}

	@Override
	@Transactional
	public boolean addStockHistory(StockHistory stockHistory) throws Exception {
		if (ObjectUtil.isNotNull(stockHistory)) {
			stockHistoryDao.save(stockHistory);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public boolean delStockHistoryById(String id) throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			StockHistory stockHistory = stockHistoryDao.get("id", id);
			if (ObjectUtil.isNotNull(stockHistory)) {
				stockHistory.setDeletedFlag(SZSCMConstant.DELETE_FLAG_YES);
				stockHistoryDao.update(stockHistory);
				return true;
			}
		}
		return false;
	}

	@Override
	public int getHistoryCountByWhNo(String whNo) {
		DetachedCriteria detachedCriteria = stockHistoryDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("whNo", whNo));
		return stockHistoryDao.getCountByCriteria(detachedCriteria);
	}
}
